package com.tianhua.javs.core.factory;

import com.tianhua.javs.client.enums.BusinessKeyWardEnum;
import com.tianhua.javs.client.enums.JavsKeyWardEnum;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

/**
 * Description:断言工厂
 * date: 2022/2/11
 *
 * @author shenshuai
 * @version 1.0.0
 * @since JDK 1.8
 */
@Service
public class PredicateFactory {
    //javs关键字或者函数关键字的断言工厂
    private static List<Predicate<String>> javsShellPredicationList = new ArrayList<>();

    //业务关键字或者函数关键字的断言工厂
    private static List<Predicate<String>> businessShellPredicationList = new ArrayList<>();

    static {

        for (JavsKeyWardEnum keyWardEnum : JavsKeyWardEnum.values()){
            Predicate<String> predicate = str -> str.trim().startsWith(keyWardEnum.getKeyWardCode());
            javsShellPredicationList.add(predicate);
        }

        for (BusinessKeyWardEnum keyWardEnum : BusinessKeyWardEnum.values()){
            Predicate<String> predicate = str -> str.trim().startsWith(keyWardEnum.getKeyWardCode());
            businessShellPredicationList.add(predicate);
        }
    }

    /**
     * 根据断言工厂判断是否是是一小段以javs关键字或者关键字函数开头的
     * 脚本代码段
     * @param javsShellSegment
     * @return
     */
    public boolean isJavsKeyWard(String javsShellSegment){
        for (Predicate predicate : javsShellPredicationList){
            if(predicate.test(javsShellSegment)){
                return true;
            }
        }
        return false;
    }


    /**
     * 根据断言工厂判断是否是是一小段以业务关键字开头的
     * 脚本代码段
     * @param javsShellSegment
     * @return
     */
    public boolean isBusinessKeyWard(String javsShellSegment){
        for (Predicate predicate : businessShellPredicationList){
            if(predicate.test(javsShellSegment)){
                return true;
            }
        }
        return false;
    }

    /**
     * 判断当前宏定义属性是否是springbean的属性
     * @param macorFieldSegment 宏定义变量部分
     * @return
     */
    public boolean isSpringBeanField(String macorFieldSegment){
        String tmp = macorFieldSegment.trim();
        return tmp.endsWith(".resource") || tmp.endsWith(".resource()")
                || tmp.endsWith("autowired") || tmp.endsWith("reference");

    }


}
